import axios from 'axios';
import { Message } from '@alifd/next';

// Set baseUrl when debugging production url in dev mode
// axios.baseUrl = '//xxxx.taobao.com';


export default async function request(options) {
    try {
        const response = await axios(options);
        const data = response.data;
        if (data.code === 200) {
            return { response, data };
        } else if (data.code === 401) {
            // 处理未登录逻辑
            location.href = '#';
        } else {
            throw new Error(data.msg || '后端接口异常');
        }
    } catch (err) {
    // 统一处理接口异常逻辑
        const response = err?.response;
        if (response && response.data) {
            Message.error(response.data?.msg || '后端接口异常');
        } else {
            Message.error(err?.message || '后端接口异常');
        }
        return Promise.reject();
    }
}
